﻿
(function ($) {
    app.modals.CreateOrEditLineModal = function () {

        var _modalManager;

        var _lineService = abp.services.app.line;

        //$(".maxlength-handler").maxlength({
        //    limitReachedClass: "label label-danger",
        //    alwaysShow: true,
        //    threshold: 5,
        //    placement: 'bottom'
        //});

        var _$lineInformationForm = null;

        function _findAssignedAlcCodeNames() {
            var assignedAlcCodeNames = [];

            _modalManager.getModal()
                .find('.user-role-checkbox-list input[type=checkbox]')
                .each(function () {
                    if ($(this).is(':checked')) {
                        assignedAlcCodeNames.push($(this).attr('name'));
                    }
                });

            return assignedAlcCodeNames;
        }


        this.init = function (modalManager) {
            _modalManager = modalManager;
            _$lineInformationForm = _modalManager.getModal().find("form[name=lineInformationsForm]");
            //渲染select
            $("#factoryIdEdit").selectpicker();
            $("#workshopIdEdit").selectpicker();
        }

        //下拉框二级联动
        $("#factoryIdEdit").change(function () {
            //获取工厂的ID
            var factoryID = $("#factoryIdEdit").val();
            //获取车间
            var lst = $("#workshopIdEdit");
            $.ajax({
                type: "get",
                url: "/Workshops/GetAllWorkshopByFacoryId",
                data: { id: factoryID },
                dataType: 'JSON', //返回数据的格式
                contentType: 'application/json',
                success: function (data, status) {
                    if (status == "success") {
                        lst.empty();//清空下拉框
                        $.each(data, function (i, item) {
                            lst.append("<option value='" + data[i].Value + "'>" + data[i].DisplayText + "</option>");
                        });
                        lst.selectpicker('refresh');
                    }
                },
                error: function () {
                    toastr.error('Error');
                },
            });
        });

        this.save = function () {
            if (!_$lineInformationForm.valid()) {
                return;
            }
            //校验通过
            var assignedAlcCodeNames = _findAssignedAlcCodeNames();
            var line = _$lineInformationForm.serializeFormToObject();
            //  console.log(line);

            _modalManager.setBusy(true);

            _lineService.createOrUpdateLineAsync({
                lineEditDto: line,
                assignedAlcCodeNames: assignedAlcCodeNames,
            }).done(function () {
                //提示信息
                abp.notify.info(app.localize('SavedSuccessfully'));
                //关闭窗体
                _modalManager.close();
                //信息保存成功后调用事件，刷新列表
                abp.event.trigger('app.createOrEditLineModalSaved');
            }).always(function () {
                _modalManager.setBusy(false);
            });
        }
    }
})(jQuery);

